﻿using System;
using System.Collections.Generic;
using Timeline.Providers;

namespace Timeline.Inis {
    public class GeneralIni {
        public GeneralIni(string id) {
            Id = id;
        }

        // 非null
        public string Id { get; set; }

        // 分组
        public int Group { get; set; }

        // 图源名
        public string Name { get; set; }

        // 图源描述
        public string Slogan { get; set; }

        // 图片数据API（可为空、一个或多个）
        public string GalleryApi { get; set; }

        // 标题规则
        public string TitleRule { get; set; }

        // 副标题规则
        public string CaptionRule { get; set; }

        // 排序组（非null）
        private readonly List<string> orders = new List<string>();
        public List<string> Orders {
            set {
                orders.Clear();
                if (value != null) {
                    orders.AddRange(value);
                }
            }
            get => orders;
        }

        // 排序（可为null）
        private string order;
        public string Order {
            get => order;
            set {
                if (value == "latest") {
                    order = "date";
                } else if (value == "trending") {
                    order = "score";
                } else {
                    order = value;
                }
            }
        }

        // 大类组（非null）
        private readonly List<string> cateHows = new List<string>();
        public List<string> CateHows {
            set {
                cateHows.Clear();
                if (value != null) {
                    cateHows.AddRange(value);
                }
            }
            get => cateHows;
        }

        // 大类（非null，""为全部）
        private string cateHow = "";
        public string CateHow {
            set => cateHow = value ?? "";
            get => cateHow;
        }

        // 子类组（非null）
        private readonly List<string> cateWhats = new List<string>();
        public List<string> CateWhats {
            set {
                cateWhats.Clear();
                if (value != null) {
                    cateWhats.AddRange(value);
                }
            }
            get => cateWhats;
        }

        // 子类（非null，""为全部）
        private string cateWhat = "";
        public string CateWhat {
            set => cateWhat = value ?? "";
            get => cateWhat;
        }

        // 桌面背景推送周期（0.25~24）
        private float desktopPeriod = 24;
        public float DesktopPeriod {
            set => desktopPeriod = Math.Max(Math.Min(value, 24), 0.25f);
            get => desktopPeriod;
        }
        // 锁屏背景推送周期（0.25~24）
        private float lockPeriod = 24;
        public float LockPeriod {
            set => lockPeriod = Math.Max(Math.Min(value, 24), 0.25f);
            get => lockPeriod;
        }

        public virtual GeneralProvider GenerateProvider() => new GeneralProvider { Id = this.Id };

        override public string ToString() => $"desktopperiod={DesktopPeriod}&lockperiod={LockPeriod}" +
            $"&order={Order}&catehow={CateHow}&catewhat={CateWhat}";
    }

    public class LocalIni : GeneralIni {
        public LocalIni(string id) : base(id) { }

        // 导入量
        private int appetite = 10;
        public int Appetite {
            set => appetite = value <= 0 ? 10 : (value > 99 ? 99 : value);
            get => appetite;
        }

        public override GeneralProvider GenerateProvider() => new LocalProvider { Id = this.Id };
    }

    public class GluttonIni : GeneralIni {
        public GluttonIni(string id) : base(id) { }

        public override GeneralProvider GenerateProvider() => new GluttonProvider { Id = this.Id };
    }

    public class BingIni : GeneralIni {
        public BingIni(string id) : base(id) { }

        public override GeneralProvider GenerateProvider() => new BingProvider { Id = this.Id };
    }

    public class Himawari8Ini : GeneralIni {
        public Himawari8Ini(string id) : base(id) { }

        // 位置（默认居中）
        private float offset = 0.5f;
        public float Offset {
            set => offset = value < 0.01f ? 0.01f : (value > 1 ? 1 : value);
            get => offset;
        }

        // 大小（默认50%）
        private float ratio = 0.5f;
        public float Ratio {
            set => ratio = value < 0.1f ? 0.1f : (value > 1 ? 1 : value);
            get => ratio;
        }

        public override GeneralProvider GenerateProvider() => new Himawari8Provider { Id = this.Id };
    }
}
